package com.ruoyi.workflow.util;

import org.activiti.engine.HistoryService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.runtime.ProcessInstance;

public class ActivitiUtils {

    private static ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    /**
     * 通过runtimeService 根据流程定义获取 businessKey
     * @param processInstanceId 流程实例ID
     * @return
     */
    public static String getBusinessKeyByRuntimeProcessInstanceId(String processInstanceId){
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //获取流程实例
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        //根据流程实例获取businessKey
        String businessKey = processInstance.getBusinessKey();
        return businessKey;
    }

    /**
     * 通过HistoryService 根据流程定义获取 businessKey
     * @param processInstanceId 流程实例ID
     * @return
     */
    public static String getBusinessKeyByHistoryProcessInstanceId(String processInstanceId){
        HistoryService historyService = processEngine.getHistoryService();
        //获取流程实例
        HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        //根据流程实例获取businessKey
        String businessKey = historicProcessInstance.getBusinessKey();
        return businessKey;
    }
}
